package com.scitc.pleasurevideo.woview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.adapter.SettingVideoSourceListAdapter;
import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.datasql.VideoSourceSqlData;
import com.scitc.pleasurevideo.okgoutil.HttpCompleteOrFail;
import com.scitc.pleasurevideo.okgoutil.OkGoUtil;
import com.scitc.pleasurevideo.pojo.VideoSourceVo;
import com.scitc.pleasurevideo.util.ConstUtil;

import java.util.List;

public class SettingVideoSourceListActivity extends ETActivity {

    private RecyclerView recyclerView;
    private SettingVideoSourceListAdapter madapter;
    private TextView tvCheckUpdateList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_video_source_list);
        initView();
        onclick();
    }

    /**
     * 点击事件
     */
    private void onclick() {
        //重新请求网络，重新加载视频网址列表
        tvCheckUpdateList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //加载视频播放网址列表
                OkGoUtil.init().getVideoSourceList(new HttpCompleteOrFail() {
                    @Override
                    public void onComplete(String json) {
                        try {
                            List<VideoSourceVo> videoSourceVos= new Gson().fromJson(json, new TypeToken<List<VideoSourceVo>>(){}.getType());
                            if(videoSourceVos==null||videoSourceVos.size()<1) {
                                Toast.makeText(mInstance, "没有数据，请与管理员联系", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            madapter.addAll(videoSourceVos);
                            VideoSourceSqlData.getDbDataManager().saveAllVideoSourceFrist(videoSourceVos);
                        } catch (JsonSyntaxException e) {
                                Toast.makeText(mInstance, "没有数据，请与管理员联系", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFile(String information) {
                                Toast.makeText(mInstance, "没有数据，请与管理员联系", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    /**
     * 初始化
     */
    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mInstance));
        List<VideoSourceVo> items = VideoSourceSqlData.getDbDataManager().selectVideoSourceList();
        madapter = new SettingVideoSourceListAdapter(mInstance, items);
        recyclerView.setAdapter(madapter);
        tvCheckUpdateList = (TextView) findViewById(R.id.tvCheckUpdateList);
    }

    @Override
    public void finish() {
        getVideoSelectStr();
        super.finish();
    }

    /**
     * 回到设置了选中了几个网址资源
     */
    private void getVideoSelectStr() {
        @SuppressLint("DefaultLocale") String string = String.format("%d/%d", madapter.getSelectItemCount(), madapter.getItemCount());
        List<VideoSourceVo> videoSourceVos=madapter.getItems();
        if(videoSourceVos!=null && videoSourceVos.size()>0){
            VideoSourceSqlData.getDbDataManager().saveAllVideoSource(videoSourceVos);
        }
        onVideoSourceNumberStr.onVideoSourceNumberStr(string);
    }

    private static OnVideoSourceNumberStr onVideoSourceNumberStr;

    public interface OnVideoSourceNumberStr {
        void onVideoSourceNumberStr(String selectStr);
    }

    public static void setOnVideoSourceNumberStr(OnVideoSourceNumberStr onVideoSourceNumberStr) {
        SettingVideoSourceListActivity.onVideoSourceNumberStr = onVideoSourceNumberStr;
    }
}
